package top.whysu.edevice.websocket.config;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;

import java.security.Principal;
import java.util.Map;

/**
 * websocket处理器
 */
@Component
public class PrincipalDefaultHandshakeHandler extends DefaultHandshakeHandler {

    @Override
    protected Principal determineUser(ServerHttpRequest request, WebSocketHandler wsHandler, Map<String, Object> attributes) {
        //key就是服务器和客户端保持一致的标记，一般可以用账户名称，或者是用户ID。
        return new Principal() {
            @Override
            public String getName() {
                return attributes.get("WEBSOCKET_SESSIONID").toString();
            }
        };
    }
}
